// CSE 142, Winter 2008, Marty Stepp // // This program reads a file of student midterm scores and // displays the score counts as a histogram, along with the // median (middle) score in the course. import java.io.*; import java.util.*; public class Midterm { public static void main(String[] args) throws FileNotFoundException { // Read the input file and tally up how many got each score int[] tally = new int[101]; Scanner input = new Scanner(new File("midterm.txt")); int students = 0; while (input.hasNextInt()) { int score = input.nextInt(); tally[score]++; students++; } // Print a text histogram of scores, and compute the median. int seen = 0; int median = 0; for (int i = 0; i < tally.length; i++) { System.out.print(i + ": "); // for each person who got a score of i, print a star. // There were tally[i] people who got a score of i for (int j = 0; j < tally[i]; j++) { System.out.print("*"); seen++; if (seen == students / 2) { // we have seen half the students, // so this number must be the median median = i; } } System.out.println(); } System.out.println("Median = " + median); } }